<?php
class UserController extends Agitum_Controller_Action
{
    public function loginAction()
	{
	    $user = Zend_Registry::get('Logged_User');
	    if ( $user->id > Agitum_Model_User::GUEST_USER_ID ) {
	        $this->_helper->viewRenderer->setNoRender(true);
	        return;
    	}
    	
    	$form = $this->_helper->formLoader("login");
		if ( $this->_request->isPost() ) {
	        if ($form->isValid($this->_request->getPost())) {
		        $username = $form->getValue("username");
		        $password = $form->getValue("password");

		        $user = new Agitum_Model_User();
		        $user->setName($username);
		        $user->setPassword($password);

		        $auth = Zend_Auth::getInstance();
		        $result = $auth->authenticate($user);
		        if ($result->isValid()){
	    			$referer = $this->_request->getServer('HTTP_REFERER');
					$this->_redirect( ($referer ? $referer : '/')  );
		        } else {
		        	$this->view->errorCode = $result->getCode();
		        	$this->view->errorMsg = $result->getMessages();
		        }
	        }
    	}
    	$this->view->form = $form;
    	$this->_helper->viewRenderer->setResponseSegment('userlogin');
	}

	public function logoutAction() 
	{
		$this->_helper->viewRenderer->setNoRender(true);

    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();

    	$referer = $this->_request->getServer('HTTP_REFERER');
		$this->_redirect( ($referer ? $referer : '/')  );
	}
	
	public function lostpwAction() 
	{
	}

}